﻿using Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using Tools;

namespace Admin.业务界面
{
    public partial class 管理员数据 : Form
    {
        List<Admin_1> all_adm;
        int pagenumber = 1;
        int pageSize = 20;
        int totalCount = 0;
        int pageCount = 0;
        bool changed_one;

        public 管理员数据()
        {           

            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void 管理员数据_Load(object sender, EventArgs e)//加载页面
        {
            pagenumber = 1;
            getPage(ref totalCount);
        }

        private void button5_Click(object sender, EventArgs e)//导入管理员数据
        {
            if (openFileDialog1.ShowDialog() != DialogResult.OK)
                return;

            string Path = openFileDialog1.FileName;
            DataTable dt = Tools.Read_excel.read_excel(Path);
            if (dt == null) return;

            List<Admin_1> admins = new List<Admin_1>();

            foreach (DataRow dr in dt.Rows)
            {
                Admin_1 adm = new Admin_1();
                adm.admin_uname = dr["管理员名称"].ToString();                
                adm.pwd = "123";
                admins.Add(adm);
            }
            BLL.B_Admin b_adm = new BLL.B_Admin();

            try
            {
                var res = b_adm.Add_A(admins);
                pagenumber = 1;
                getPage(ref totalCount);
            }
            catch (Tools.Add_Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }

        private void button9_Click(object sender, EventArgs e)//删除管理员数据
        {
            List<Admin_1> adm_deleted = new List<Admin_1>();
            foreach (DataGridViewRow row in dataGridView1.SelectedRows)
            {
                adm_deleted.Add(row.DataBoundItem as Admin_1);
            }
            BLL.B_Admin b_admin = new BLL.B_Admin();
            try
            {
                if (b_admin.Del_A(adm_deleted))
                    MessageBox.Show("删除成功！");
                getPage(ref totalCount);
            }
            catch (Tools.Del_Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void button8_Click(object sender, EventArgs e)//保存修改
        {
            BLL.B_Admin b_admin = new BLL.B_Admin();
            try
            {
                if (b_admin.Save_List_A(all_adm))
                    MessageBox.Show("保存成功！");
            }
            catch (Tools.Modify_Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        void getPage(ref int totalCount)//换页功能
        {
            BLL.B_Admin b_admin = new BLL.B_Admin();

            all_adm = b_admin.GetPage_A(pagenumber, pageSize, ref totalCount);
            dataGridView1.DataSource = new BindingList<Admin_1>(all_adm);

            pageCount = Convert.ToInt32(Math.Ceiling((double)totalCount / pageSize));
        }

        private void 管理员数据_FormClosed(object sender, FormClosedEventArgs e)
        {
            //this.Hide();
            //管理员主界面 f1 = new 管理员主界面();
            //f1.ShowDialog();
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex != 2) 
                return;

            changed_one = true;
        }

        private void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex != 2)
                return;

            if (changed_one)
            {
                dataGridView1.CurrentCell.Value = MD5Provider.Hash(dataGridView1.CurrentCell.Value.ToString());
                changed_one = false;
            }
        }
    }
}
